﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Site.WebService;
using Site.Models;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Site.Controllers
{
    public class GameController : Controller
    {
        private BMSiteDBEntities _db = new BMSiteDBEntities();
        //
        // GET: /Game/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Game/GameRoom/11
        public ActionResult GameRoom(int id)
        {
            if (Session.Contents["uid"] == null)
            {
                Session.Clear();
                return RedirectToAction("LogOff", "Account");
            }

            ProxyService proxy = ProxyService.getInstance();
            if (!proxy.isConnected) return View();
            // if the player is not registred in the webservice
            
            int player_id;

            if ((int)Session.Contents["uid"] == 0)
            {
                //Update player
                player_id = proxy.createPlayer(Session.Contents["user_id"].ToString(), "http://" + Request.ServerVariables["SERVER_NAME"], User.Identity.Name);
                if (player_id == 0)
                    throw new Exception("A user already registered with this name");
                // TODO register player id
                Session.Contents["uid"] = player_id;

                //Update player ws id in site db
                Guid user_id = new Guid(Session.Contents["user_id"].ToString());
                var currentUser = _db.aspnet_Users.First(m => m.UserId == user_id);
                currentUser.USER_WS_ID = (int)player_id;
                _db.SaveChanges();
            }
            player_id = (int)Session.Contents["uid"];

            if (player_id == 0)
            {
                throw new Exception("User is not registered in the WebService");
            }

            // if player is not queued
            if (!proxy.isQueued((uint)player_id))
            {
                // we queue the player
                proxy.addPlayerInRankedQueue((uint)player_id, (ServiceReferenceBomberman.MatchType)id);
                
                return View();
            }

            // if player is already in queue
            return CheckGame();
        }

        private ActionResult CheckGame()
        {
            // TODO
            ProxyService proxy = ProxyService.getInstance();
            if (!proxy.isConnected)
                // fail
                return View();
            // check if game is created
            FlashVars vars;
            int sessionId = (int)Session.Contents["uid"];
            uint sessionUid = (uint)sessionId;

            if (!proxy.getFlashVars(sessionUid, out vars))
                // fail
                return View();

            // we got the vars
            ViewData["id"] = vars.Token.ToString();
            ViewData["address"] = "192.168.2.50";
            ViewData["port"] = vars.Port.ToString();

            return View(ViewData);
        }
        public ActionResult test()
        {
            Random r = new Random();
            string test = r.Next(2, 5).ToString();
            ViewData["vars"] = test ;
            
            return View(ViewData);
        }
        

        //
        // GET: /Game/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Game/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Game/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Game/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Game/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Game/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
